package demo2.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import demo2.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    public String sayHi(String name){
        String result = null;
        //设置资源入口
        Entry entry = null;
        try{
            //给entry赋值
            entry = SphU.entry("sayHi1");
            result = helloService.sayHi(name);
        }catch (BlockException e){
            return "sorry"+name+",服务资源被降级处理了,entry赋值失败.";
        }finally {
            //释放
            if (entry!=null){
                entry.exit();
            }
        }
        return result;
    }
}
